<?php
/**
 *@fileoverview: [群博客] 
 *@author: 辛少普 <shaopu@staff.sina.com.cn>
 *@date: Mon Dec 06 02:15:14 GMT 2010
 *@copyright: sina
 */
//  ini_set('display_errors', 1);   error_reporting(E_ALL);
    class ml_biz_pagemaker extends ml_biz_htmlmaker 
    {
        const RUN_STATUS_OK = 1;                            //运行正常
        const RUN_STATUS_WARNING = 2;                        //出现异常
        const RUN_STATUS_ERROR = 0;                            //出现异常
        
        const MODULE_LEVEL_COMMON = 0;
        const MODULE_LEVEL_CORE = 0;
        const MERGE_CSS_CONTENT = true;
        const MERGE_THEME_ALLCSS = true;
        
        private $_run_status = self::RUN_STATUS_OK ;        //系统运行状态 如果有问题，再考虑后续措施
        
        private $_group_id;                                    //群ID
        private $_theme_id;                                    //当前使用的主题ID
        private $_theme_config;                                //主题默认配置
        private $_user_config;                                //用户配置
        private $_page_id;                                    //给RIA的页面ID
        
        private $_controller;                                //页面CONTROLLER
        
        private $_meta_keyword = array();                    //页面关键字
        private $_meta_description = array();                //页面关键字
        private $_blog_scope = array();                        //页面SCOPE变量
        private $_mod_css = array();                        //各模块所需要的CSS
        public  $_mod_html = array();                        //各模块产出的HTML片段
        
        
        private $_data = array();                            //外围传入数据池 0为公用 其他以模块ID为key     array({模块ID}=>array({key}=>{data}))
        
        /**
         * 构造函数
         *
         * @param int $group_id
         * @param int $theme_id
         * @param array $user_config
         */
        public function __construct($cfgName)
        {   
            $cfgName = 'ml_page_'.$cfgName;
            $this->_theme_config = ml_factory::load_theme_config($cfgName);            
            $this->_controller = ml_factory::get_controller();
        }
        /**
         * 设置页面标题
         *
         * @param string $title
         * @return void
         */
        public function set_title($title)
        {
            return parent::set_page_title($title.ML_CNF_PAGETITLE_SUFFIX);
        }
        /**
         * 设置页面PAGEID
         *
         * @param string $page_id
         */
        public function set_page_id($page_id)
        {
            return $this->add_scope_var('$pageid' , $page_id);
        }
        /**
         * 设置页面 SSO描述
         *
         * @param string $string
         * @return unknown
         */
        public function add_page_description($string)
        {
            $this->_meta_description[] = $string;
        }
        /**
         * 增加页面 SSO关键字
         *
         * @param unknown_type $string
         */
        public function add_page_keyword($string)
        {
            $this->_meta_keyword[] = $string;
            return ;
        }
        /**
         * 增加SCOPE变量数据项
         *
         * @param string $key
         * @param mix $value
         */
        public function add_scope_var($key , $value)
        {
            $this->_blog_scope[$key] = $value;
            return ;
        }
        /**
         * 增加页面引用CSS
         *
         * @param string $name
         */
        public function add_mod_css($name)
        {
            $this->_mod_css[] = $name;
            return ;
        }
        
        /**
         * 增加模块私用数据
         *
         * @param int $mod_id
         * @param string $key
         * @param mix $value
         */
        public function add_module_data($mod_id , $key , $value)
        {
            $this->_data[$mod_id][$key] = $value;
            return ;
        }
        /**
         * 增加公用数据
         *
         * @param string $key
         * @param mix $value
         */
        public function add_public_data($key , $value)
        {
            $this->_data[0][$key] = $value;
            return ;
        }
        
        /**
         * 执行
         *
         */
        public function run()
        {
            global $GB_SYS_MOD_LIST;        //所有系统模块的配置数据
            //预加载所有自定义模块数据
            //$aMod2type = $this->_prepare_custom_module();
            //所有模块列表 用于加载CSS
            $aModuleList = array();
            //所有模块生成的HTML 按容器的二维数组 见output方法 
            $aHtml = array();
            //处理页面美化
//            $this->_prepare_page_beauty(); 
            //所有容器
            
            foreach ($this->_theme_config['module'] as $k => $v) {
                $mod_id = $v['id'];     
                $sc_size = $this->_theme_config['sc_size'][$k];                
                $oModule = ml_factory::load_page_module($mod_id);
                $oModule->set_page_maker($this);
                
                //设置模块尺寸
                $oModule->set_size($sc_size);
//                $mod_data = array_merge((array)$this->_data[0] , (array)$this->_data[$mod_id]);
                $mod_data = (array)$this->_data;
                $oModule->set_module_data($mod_data);
                
                //运行模块
                $oModule->run();
                /*
                if(!$oModule->get_module_run_status())
                {
                    if($oModule->get_module_level() == self::MODULE_LEVEL_CORE )
                    {
                        $this->set_run_status(self::RUN_STATUS_ERROR);
                        return false;
                    }
                    else 
                    {
                        $this->set_run_status(self::RUN_STATUS_WARNING);
                    }
                }*/
                        
                $aHtml[] = $oModule->get_result();
            }
            
            $this->_mod_html = $aHtml;
            return true;
        }
        /**
         * 输出页面
         *
         * @param array $modules        //页面模块HTML数组     array([sc_id] => array())
         */
        public function output()
        {
            $modules = &$this->_mod_html;
            
            $theme_id = 1;
/*            $visitor = $this->_controller->__visitor;
            $is_admin = $this->_controller->__is_admin;
//            $this->add_scope_var('group_owner_uid' , (array)$this->_controller->__groupinfo['uid']);
            */
            $page_public_data = $this->_data[0]; 
            $scope_js = 'var scope = '.json_encode($this->_blog_scope).';';
            $this->add_output_js($scope_js, 'top');
            
            $this->add_meta('keywords' , implode(',' , array_unique($this->_meta_keyword)));
            $this->add_meta('description' , implode(',' , array_unique($this->_meta_description)));
            $meta = $this->get_meta();
            $css = $this->get_css();
            $js = $this->get_js();

            $page_title = $this->get_page_title();
            $page_id = $this->_page_id;
            
            
            //输出禁用缓存HEADER
            ml_tool_httpheader::no_cache();
            include(SERVER_ROOT_PATH.'/view/tpl_gb_front_html.php');
            die;
        }
        
        /**
         * 设置页面执行状态
         *
         * @param int $status
         */
        public function set_run_status($status)
        {
            $this->_run_status;
        }
        public function get_run_status()
        {
            return $this->_run_status;
        }
       
        /**
         * 根据模块列表模式化CSS
         *
         * @param array $aModule
         */
        private function _format_css($aModule)
        {
            include(SERVER_ROOT_PATH.'/config/gb_mod2css.php');
            
            //加载当前主题下的所有CSS
            if(self::MERGE_THEME_ALLCSS)
                ; //$aCssName = ml_factory::load_theme_allcss_name($this->_theme_id);
            else 
            {
                $aCssName = array();
                foreach ($aModule as $mod)
                {
                    if($GB_MOD2CSS[$mod])
                    {
                        $aCssName[] = $GB_MOD2CSS[$mod];
                    }
                }
                if(count($this->_mod_css))
                {
                    foreach ($this->_mod_css as $css_name)
                    {
                        $aCssName[] = $css_name;
                    }
                }
            }
            
            $aCssName[] = 'footer';
            $aCssName = array_unique($aCssName);
            
            /*
            if(self::MERGE_CSS_CONTENT)    
                $this->add_output_css(gb_css_maker::get_merged_module_css($this->_theme_id , $aCssName));
            else 
            {
               
                $aUrl = gb_css_maker::get_import_module_css($this->_theme_id , $aCssName);
                foreach ($aUrl as $url)
                    $this->add_import_css($url);
            }
            */
        }
    }
?>